// 角色管理

package basic

import (
	"api/common/web"
	"api/models"
	"net/http"
)

// @TagName 角色管理
// @Controller RoleController
type RoleController struct{}

// @Title 角色列表数据
// @Route /role/list --> List
// @Param {"getOtherData":true}
// @Method post
func (this *RoleController) List(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)
	inputJson, msg := web.TokenCheckRequest(r)
	if msg != nil {
		web.ServeJSON(w, msg)
		return
	}

	page, msg := web.JsonRootWithInput(inputJson, &models.Role{})
	if msg != nil {
		web.ServeJSON(w, msg)
		return
	}

	data := map[string]interface{}{}
	data["success"] = page.Success
	if !page.Success {
		data["msg"] = page.Msg
		web.ServeJSON(w, data)
		return
	}

	data["data"] = page.Root

	// 是否额外返回数据
	getOtherData := false
	getOtherDataProbe := inputJson["getOtherData"]
	if getOtherDataProbe != nil {
		getOtherData = getOtherDataProbe.(bool)
	}
	if !getOtherData {
		web.ServeJSON(w, data)
		return
	}

	// 菜单数据
	menuPage, msg := web.JsonRootWithInputFilter(inputJson, &models.Menu{}, "", "", "id")
	if msg != nil {
		web.ServeJSON(w, msg)
		return
	}

	data["menuData"] = menuPage.Root

	// 资源数据
	resPage, msg := web.JsonRootWithInputFilter(inputJson, &models.Resource{}, "", "", "id")
	if msg != nil {
		web.ServeJSON(w, msg)
		return
	}

	data["resData"] = resPage.Root
	web.ServeJSON(w, data)
}

// @Title 新增角色信息
// @Route /role/create --> Create
// @Method post
func (this *RoleController) Create(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)
	web.CreateRecord(w, r, &models.Role{})
}

// @Title 编辑角色信息
// @Route /role/edit --> Edit
// @Method post
func (this *RoleController) Edit(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)
	web.EditRecord(w, r, &models.Role{})
}

// @Title 删除角色信息
// @Route /role/delete --> Delete
// @Method post
func (this *RoleController) Delete(w http.ResponseWriter, r *http.Request) {
	defer web.PanicHandler(w, r)
	web.DeleteCallBak(w, r, &models.Role{})
}
